package com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.common.pojo.CommonResult;
import static com.hz.monitoring.framework.common.pojo.CommonResult.success;

import com.hz.monitoring.framework.excel.core.util.ExcelUtils;

import com.hz.monitoring.framework.operatelog.core.annotations.OperateLog;
import static com.hz.monitoring.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.hz.monitoring.module.hardware.controller.admin.monitoring_warning_log.vo.*;
import com.hz.monitoring.module.hardware.dal.dataobject.monitoring_warning_log.MonitoringWarningLogDO;
import com.hz.monitoring.module.hardware.convert.monitoring_warning_log.MonitoringWarningLogConvert;
import com.hz.monitoring.module.hardware.service.monitoring_warning_log.MonitoringWarningLogService;

@Api(tags = "管理后台 - 预警日志")
@RestController
@RequestMapping("/hardware/monitoring-warning-log")
@Validated
public class MonitoringWarningLogController {

    @Resource
    private MonitoringWarningLogService monitoringWarningLogService;

    @PostMapping("/create")
    @ApiOperation("创建预警日志")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:create')")
    public CommonResult<Long> createMonitoringWarningLog(@Valid @RequestBody MonitoringWarningLogCreateReqVO createReqVO) {
        return success(monitoringWarningLogService.createMonitoringWarningLog(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新预警日志")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:update')")
    public CommonResult<Boolean> updateMonitoringWarningLog(@Valid @RequestBody MonitoringWarningLogUpdateReqVO updateReqVO) {
        monitoringWarningLogService.updateMonitoringWarningLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除预警日志")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:delete')")
    public CommonResult<Boolean> deleteMonitoringWarningLog(@RequestParam("id") Long id) {
        monitoringWarningLogService.deleteMonitoringWarningLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得预警日志")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:query')")
    public CommonResult<MonitoringWarningLogRespVO> getMonitoringWarningLog(@RequestParam("id") Long id) {
        MonitoringWarningLogDO monitoringWarningLog = monitoringWarningLogService.getMonitoringWarningLog(id);
        return success(MonitoringWarningLogConvert.convert(monitoringWarningLog));
    }

    @GetMapping("/list")
    @ApiOperation("获得预警日志列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:query')")
    public CommonResult<List<MonitoringWarningLogRespVO>> getMonitoringWarningLogList(@RequestParam("ids") Collection<Long> ids) {
        List<MonitoringWarningLogDO> list = monitoringWarningLogService.getMonitoringWarningLogList(ids);
        return success(MonitoringWarningLogConvert.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得预警日志分页")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:query')")
    public CommonResult<PageResult<MonitoringWarningLogRespVO>> getMonitoringWarningLogPage(@Valid MonitoringWarningLogPageReqVO pageVO) {
        PageResult<MonitoringWarningLogDO> pageResult = monitoringWarningLogService.getMonitoringWarningLogPage(pageVO);
        return success(MonitoringWarningLogConvert.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出预警日志 Excel")
    @PreAuthorize("@ss.hasPermission('hardware:monitoring-warning-log:export')")
    @OperateLog(type = EXPORT)
    public void exportMonitoringWarningLogExcel(@Valid MonitoringWarningLogExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MonitoringWarningLogDO> list = monitoringWarningLogService.getMonitoringWarningLogList(exportReqVO);
        // 导出 Excel
        List<MonitoringWarningLogExcelVO> datas = MonitoringWarningLogConvert.convertList02(list);
        ExcelUtils.write(response, "预警日志.xls", "数据", MonitoringWarningLogExcelVO.class, datas);
    }

}
